class Statistics::Tweet
  include Mongoid::Document
  include Mongoid::CriteriaExt

  field :wid,            :type => String
  field :text,           :type => String
  field :comments_count, :type => String
  field :location,       :type => String
  field :created_at,     :type => Integer

  field :comments,       :type => Hash, :default => {:content => []}
  field :error,          :type => String

  field :cid,            :type => String
  field :spammer_id,     :type => String
  field :cuser,          :type => Hash, :default => {}

  field :sns_type,       :type => String
  field :url,            :type => String
  field :clicks,         :type => Hash,  :default => {}

  field :for_t,          :type => Hash,  :default => {}

  scope :success, ->{ where(:error => nil) }

  belongs_to :push_history
  belongs_to :sns_user, :class_name => 'Statistics::SnsUser'


  def present_history
    _sns_user = self.sns_user ? self.sns_user : Statistics::SnsUser.new
    present_attrs :_id, :wid, :text, :comments_count, :location, :error, :wurl, :for_t, :clicks
    present_add :created_at        => Time.at(self.created_at || 0)
    present_add :user_name         => _sns_user.screen_name
    present_add :profile_image_url => _sns_user.profile_image_url
    present_add :gender            => _sns_user.gender
    present_add :uid               => _sns_user.uid
    present_add :user_url          => user_url
    present_add :user_tweeted      => _sns_user.tweets.count
  end

  def get_comments(refresh = false)
    self.comments[:content] ||= []
    if refresh || self.comments[:content].blank?
      spammer = Utility.const_get(sns_type.capitalize).where(:uid => self.spammer_id).active.first ||
        Utility.const_get(sns_type.capitalize).get_spammer
      return false unless spammer
      _result = spammer.show_comments(self.wid, self.comments[:since] || 0)
      if _result["error_code"].blank?
        self.comments[:content] += _result["comments"]
        self.comments[:since] = _result["comments"].first["id"] if _result["comments"].present?
        save
      end
    end
    self.comments[:content]
  end

  def add_comment(comment)
    spammer = Utility.const_get(sns_type.capitalize).where(:uid => self.spammer_id).active.first ||
        Utility.const_get(sns_type.capitalize).get_spammer
    return false unless spammer
    _result = spammer.add_comment(comment, self.wid, sns_user.uid)
    if _result["error_code"].blank?
      get_comments(true)
    else
      false
    end
  end


  def track_url
    "#{Oauth2::FLOWY_URL}/ut/#{self._id}"
  end

  def set_track_url(_url)
    self.url = _url
    save
    track_url
  end

  def quill_url(flowy_token)
    self.url + "?flowy=#{flowy_token}&fid=#{_id}"
  end

  def quill_clicked(flowy_token)
    self.clicks[flowy_token]['successed_at'] = Time.now
  end

  def clicked
    flowy_token = "#{Time.now.to_i}_#{rand(10000)}"
    self.clicks[flowy_token] = {
      'clicked_at' => Time.now
    }
    save
    quill_url(flowy_token)
  end

  def clicked_count
    clicks.count
  end

  def user_url
    return "" unless sns_user
    case sns_user.type
    when 'tencent'
      "http://t.qq.com/#{sns_user.name}"
    when 'weibo'
      "http://e.weibo.com/#{sns_user.uid}"
    else
      ""
    end

  end

end